#include "spring.h"
#include "math.h"
#include "util/mmgr.h"		// Needs to come in early


using namespace math;


Spring::Spring(float TargetDist, float Strength) :
	m_TargetLength(TargetDist), 
	m_Strength(Strength)
{

}

Spring::~Spring()
{

}

math::Vec2		Spring::GetForce(const math::Vec2& From, const math::Vec2& To)
{
	const float SPRING_TOLERANCE   = (0.0000000005f);

	math::Vec2 Delta = (To-From);
	float Length = Delta.Magnitude();
	if (Length < SPRING_TOLERANCE ) return	math::Vec2(0,0);

	Delta.Normalize();
	// assume that the force is applied to m_To only
	float	Force = (m_TargetLength - Length) * m_Strength;
	return Delta * Force;
}

math::Vec2		Spring::GetPositiveForce(const math::Vec2& From, const math::Vec2& To)
{
	const float SPRING_TOLERANCE   = (0.0000000005f);

	math::Vec2 Delta = (To-From);
	float Length = Delta.Magnitude();
	if (Length < SPRING_TOLERANCE ) return	math::Vec2(0,0);

	Delta.Normalize();
	// assume that the force is applied to m_To only
	float	Force = (m_TargetLength - Length) * m_Strength;
	if (Force > 0) return math::Vec2(0, 0);

	return Delta * Force;
}
